package sports.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import sports.service.SportsService;
import sports.service.SportsRecordVO;

import java.sql.Date;
/**
 * Created by qianyang on 2018/4/26.
 */

@RestController
@RequestMapping(value = "/api/Sports")
public class SportsController {

    @Autowired
    private SportsService sportsService;

    //显示某用户、某一天的运动记录
    @RequestMapping(value = "/records", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String selectSportsRecord(Integer userId,Date date) {
        return sportsService.selectSportsRecord(userId,date);
    }

    //添加运动记录
    @RequestMapping(value = "/records", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String insertSportsRecord(@RequestBody SportsRecordVO sportsRecordVO){
        System.out.println("controller");
        return sportsService.insertSportsRecord(sportsRecordVO);
    }

    //删除运动记录
    @RequestMapping(value = "/records", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delectSportsRecord(Integer id){
        return sportsService.delectSportsRecord(id);
    }

    //更新运动记录
    @RequestMapping(value = "/recordsUpdate", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updateSportsRecord(@RequestBody SportsRecordVO sportsRecordVO){
        System.out.println("controller");
        return sportsService.updateSportsRecord(sportsRecordVO);
    }

    @RequestMapping(value = "/record", method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String selcetSportsRecordById(Integer sportsRecordId){
        System.out.println("controller");
        return sportsService.selcetSportsRecordById(sportsRecordId);
    }

    //显示某id对应的运动信息
    @RequestMapping(value = "/sports", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String selcetSportsById(Integer sportsId) {
        return sportsService.selcetSportsById(sportsId);
    }

    //显示运动列表
    @RequestMapping(value = "/sportsList", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String selcetALLSports() {
        return sportsService.selcetALLSports();
    }

    //搜索运动
    @RequestMapping(value = "/search", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String searchSportsByNmae(String name) {
        System.out.println("in controller");
        return sportsService.searchSportsByNmae(name);
    }

}
